<?php

namespace App\Models;

use Session;
use Illuminate\Support\Facades\DB;
class Invite extends BaseModel
{
    public $timestamps = false;
    protected $table = "invite";
    public function getOne($where, $fileds = '*', $order = '')
    {
        if (!$order) {
            $order = ['id' => 'desc'];
        }
        return $this->multiSelect($fileds)->multiWhere($where)->multiOrder($order)->first();
    }

    /**
     * 按条件查询全部数据,根据配置显示条数显示
     */
    public function getList(array $where = [], $fields = '*', $order = '', $pageSize = '')
    {
        if ($pageSize) {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->paginate($pageSize);
        } else {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->get();
        }
    }

    /**
     *插入数据
     */
    public function add($data)
    {
        return $this->_add($data);
    }


    /***
     * @param array $id
     * @param array $data
     * 更新数据
     */
    public function up($where, $data)
    {
        return $this->_updata($where, $data);
    }

    /**
     * @param $id
     * 删除数据
     */
    public function del($where)
    {
        return $this->_del($where);
    }


    /**
     * 创建url
     */
    public function createInviteUrl(){
           $member = new Member();
           $user = $member->getMemberById(session('uid'));
           $url = $_SERVER['SERVER_NAME']."/register?code=".$user['code'];
           return $url;
    }

     /**
      * 成功邀请查询
      */
    public function invited($request)
    {
           $uid = session('uid');
           $where = [];
           $order = '';
           if ($request['type'] != null && $request['type'] != 'all') {
               $where['member_id'] = $uid;
               $where['mobile'] = $request['keyword'];
           }
           $where['member_id'] = $uid;
           $res = $this->getList($where,'*',$order,5);
           if ($res) {
                $res = $res->toArray();
                $res['pageNoList'] = $this->getPageNoList($res['last_page'], request('page', 1), 10);
           }
           return $res;

     }
}

